<template>
  <el-dialog model-value="dialogVisible" :title="dialogTitle" width="30%" @close="handleClose">

    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">




      <el-form-item label="日志信息" prop="type">
        <el-input v-model="form.text" />
      </el-form-item>



    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>

import { defineEmits, defineProps, ref, watch } from "vue";
import requestUtil, { getServerUrl } from "@/utils/request";
import { ElMessage } from 'element-plus'

const props = defineProps(
  {
    id: {
      type: Number,
      default: -1,
      required: true
    },
    dialogTitle: {
      type: String,
      default: '',
      required: true
    },
    dialogVisible: {
      type: Boolean,
      default: false,
      required: true
    },
    isUpdate: {
      type: Boolean,
      default: true,
      required: false
    }
  }
)


const form = ref({
  id: -1,
  text: '',

})


const rules = ref({
  text: [{ required: true, message: "模块类型", trigger: "blur" }]
})

const formRef = ref(null)

const initFormData = async (id) => {
  const res = await requestUtil.get("/ylz/cms-log/cms-log/findByDeviceid", { id: id })
  form.value = res.data.data[0];
}



watch(
  () => props.dialogVisible,
  () => {
    let id = props.id;
    if (id != -1) {
      initFormData(id)
    } else {

      form.value = {
        id: -1,

        text: '',
      }

    }
  }
)


const emits = defineEmits(['update:modelValue', 'initUserList'])

const handleClose = () => {
  emits('update:modelValue', false)
}

const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let result;
      if (form.value.id == -1) {
        result = await requestUtil.post("/ylz/cms-log/cms-log/save", form.value);
      } else {
        result = await requestUtil.post("/ylz/cms-log/cms-log/update", form.value);
      }

      let data = result.data;
      if (data.code == 200) {
        ElMessage.success("执行成功！")
        formRef.value.resetFields();
        emits("initUserList")
        handleClose();
      } else {
        ElMessage.error(data.msg);
      }
    } else {
      console.log("fail")
    }
  })
}

</script>

<style scoped>
.demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
</style>
